package com.sc.insert;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.view.View;
import android.widget.EditText;

public class CallActivity extends AppCompatActivity {
    private EditText et_phone_number;
    private EditText et_phone_type;
    private EditText et_phone_duration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call);

        et_phone_number = findViewById(R.id.et_phone_number);
        et_phone_duration = findViewById(R.id.et_phone_duration);
        et_phone_type = findViewById(R.id.et_phone_type);
    }

    public void insertContact1(View view) {
        insertContact("1");
    }

    public void insertContact2(View view) {
        insertContact("2");
    }

    public void insertContact(String simid) {
        String[] PERMISSIONS_WRITE_CALL_LOG = {Manifest.permission.WRITE_CALL_LOG, Manifest.permission.READ_CALL_LOG};

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALL_LOG) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSIONS_WRITE_CALL_LOG, 1);
            return;
        }
        ContentResolver resolver = getContentResolver();
        Uri uri = CallLog.Calls.CONTENT_URI;
        ContentValues values = new ContentValues();
        String number = et_phone_number.getText().toString();
        String duration = et_phone_duration.getText().toString();
        String type = et_phone_type.getText().toString();
        values.put(CallLog.Calls.NUMBER, number);
        values.put(CallLog.Calls.DURATION, duration);
        values.put(CallLog.Calls.DATE, System.currentTimeMillis());
        values.put(CallLog.Calls.TYPE, type);
        values.put("simid", simid);
        resolver.insert(uri, values);
    }
}
